package com.sikeda.imagecomposetool.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.sikeda.imagecomposetool.R;
import com.sikeda.imagecomposetool.custom.ZoomImageView;
import com.sikeda.imagecomposetool.entity.ImageBean;
import com.sikeda.imagecomposetool.utils.GlideUtil;
import com.sikeda.imagecomposetool.utils.ImageLoaderUtil;
import com.sikeda.imagecomposetool.utils.PixelUtil;

import java.util.ArrayList;
import java.util.List;

import cn.finalteam.galleryfinal.model.PhotoInfo;
import cn.finalteam.galleryfinal.widget.zoonview.PhotoView;

/**
 * Created by Administrator on 2016/12/13.
 */

public class GridPhotoViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<String> list;
    private Context context;
    private CustomItemOnClickListener listener;
    private int totalWidth;

    public GridPhotoViewAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
        totalWidth = PixelUtil.getScreenWidth((Activity) context);
    }

    public void setData(List<String> list) {
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View viewRoot = LayoutInflater.from(context).inflate(R.layout.item_grid_photoviews, parent, false);
        final ItemViewHolder itemViewHolder = new ItemViewHolder(viewRoot);
        if (listener != null) {
            viewRoot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.setOnItemClickListener(itemViewHolder.getLayoutPosition());
                }
            });
        }
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewGroup.LayoutParams layoutParams = ((ItemViewHolder) holder).photoView.getLayoutParams();
        layoutParams.width = totalWidth / 3;
        layoutParams.height = layoutParams.width;
        ((ItemViewHolder) holder).photoView.setLayoutParams(layoutParams);
        ImageLoaderUtil.displayFromSDCard(list.get(position), ((ItemViewHolder) holder).photoView);
        GlideUtil.loadFile(list.get(position), ((ItemViewHolder) holder).photoView);
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private ImageView photoView;

        public ItemViewHolder(View itemView) {
            super(itemView);
            photoView = (ImageView) itemView.findViewById(R.id.photoview);
        }
    }

    public void setCustomItemOnClickListener(CustomItemOnClickListener listener) {
        this.listener = listener;
    }

    public interface CustomItemOnClickListener {
        void setOnItemClickListener(int pos);
    }
}
